const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const url = require('url');
const publicPath = '';

const pageConfig = require('./config/pageConfig.js');

let entrys = {vendor: './src/entry/vendor.js'};
let htmlPlugins = [];

pageConfig.forEach((page) => {
    entrys[page.name] = page.entrySrc;
    htmlPlugins.push(new HtmlWebpackPlugin({
        template: page.template || 'src/index.html',
        title: page.title || '数据智库操作员管理平台',
        filename: page.filename || page.name + '.html',
        chunks: ['vendor', 'manifest', page.name]
    }))
});

module.exports = (options = {}) => ({
    entry: entrys,
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
        chunkFilename: '[id].js?[chunkhash]',
        publicPath: publicPath
    },
    module: {
        rules: [{
            test: /\.vue$/,
            use: ['vue-loader']
        },
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env'],
                        plugins: ['transform-runtime']
                    }
                },
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader', 'postcss-loader']
            },
            {
                test: /\.scss$/,
                use: ['style-loader', 'css-loader', 'sass-loader']
            },
            {
                test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 10000
                    }
                }]
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest']
        })
    ].concat(htmlPlugins),
    resolve: {
        alias: {
            components: path.resolve(__dirname, './src/components'),
            common: path.resolve(__dirname, './src/common'),
            style: path.resolve(__dirname, './src/style'),
            plugin:  path.resolve(__dirname, './src/components/plugin'),
        }
    },
    devServer: {
        host: '127.0.0.1',
        port: 8080,
        contentBase: '/dist',
        proxy: [{
            context: ["/business", "/OSSClientFileImport"],
            target: "http://116.62.148.213:9000",
            changeOrigin: true
        }]
    }
});